package prsnl.daan.ledbak.engine;

import java.awt.image.BufferedImage;

import javax.imageio.ImageIO;

public class Resources{
	public static final Bitmap	gfx			= loadBitmap( "/gfx.png" );
	
	public static final int		NOCOLOR		= 0xffff00ff;
	public static final int		GRADES		= 0x0f;
	
	private static final int	MULTIPLIER	= 0xff / GRADES;
	
	public static Bitmap loadBitmap( String fileName ) {
		try{
			BufferedImage img = ImageIO.read( Resources.class.getResource( fileName ) );
			
			int w = img.getWidth( );
			int h = img.getHeight( );
			
			Bitmap result = new Bitmap( w, h );
			img.getRGB( 0, 0, w, h, result.getPixels( ), 0, w );
			
			for(int i = 0; i < result.getPixels( ).length; i++){
				int in = result.getPixel( i );
				int col = 255 - ( ( in & 0xfe ) );
				if(in == NOCOLOR)
					col = -1;
				result.setPixel( i, col );
			}
			
			return result;
		} catch(Exception e){
			throw new RuntimeException( e );
		}
	}
	
	/**
	 * 
	 * @param color
	 * @return
	 */
	public static int getColor( int color ) {
		return getColor( color, MULTIPLIER, 0xff );
	}
	
	/**
	 * 
	 * @param color
	 * @param shade
	 * @return
	 */
	public static int getColor( int color, int shade ) {
		return getColor( color, shade, 1 );
	}
	
	/**
	 * 
	 * @param color
	 * @param shade
	 * @param divisor
	 * @return
	 */
	public static int getColor( int color, int shade, int divisor ) {
		int r = ( color >> 16 ) & 0xff;
		int g = ( color >> 8 ) & 0xff;
		int b = ( color >> 0 ) & 0xff;
		
		r = ( r * shade ) / divisor;
		g = ( g * shade ) / divisor;
		b = ( b * shade ) / divisor;
		
		return r << 16 | g << 8 | b;
	}
}
